<!-- eslint-disable vue/html-indent -->
<!-- eslint-disable vue/max-attributes-per-line -->
<!-- eslint-disable vue/html-closing-bracket-newline -->
<template>
  <div v-loading="loading" class="dashboard-container">
    <div class="app-container">
      <!-- 组织架构内容 -->
      <el-card class="tree-card">
        <!-- 用了一个行列布局 -->
        <TreeTools :treeNode="company" :is-root="true" @addDepts="addDepts" />
        <el-tree :data="departs" :props="defaultProps" :default-expand-all="true">
          <TreeTools slot-scope="{ data }" :treeNode="data" @delDepts="getDepartments" @addDepts="addDepts"
            @editDepts="editDepts" />
        </el-tree>
      </el-card>
    </div>
    <AddDept ref="addDept" :showDialog.sync="showDialog" :treeNode="node" @addDepts="getDepartments"></AddDept>
  </div>
</template>

<script>
import TreeTools from "./components/tree-tools.vue"
import { getDepartments } from "@/api/departments"
import { tranListToTreeData } from "../../utils"
import AddDept from './components/add-dept.vue'

export default {
  components: {
    TreeTools,
    AddDept
  },
  data() {
    return {
      company: { name: "", manager: "" },
      departs: [],
      defaultProps: {
        label: "name",
      },
      showDialog: false,
      node: null,
      loading: false
    };
  },
  created() {
    this.getDepartments();
  },
  methods: {
    async getDepartments() {
      this.loading = true
      const res = await getDepartments();
      this.company = {
        name: res.companyName,
        manager: "负责人",
        id: ''
      }
      this.departs = tranListToTreeData(res.depts, "")
      this.loading = false
    },
    addDepts(node) {
      this.showDialog = true
      this.node = node
    },
    editDepts(node) {
      this.showDialog = true
      this.node = node
      this.$refs.addDept.getDepartmentDetail(node.id)
    }
  }
};
</script>

<style scoped>
.tree-card {
  padding: 30px 140px;
  font-size: 14px;
}
</style>
